package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Page;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specification")
public class SpecificationController {

    @Reference
    private SpecificationService specificationService;

    @RequestMapping("list")
    public String list(){
        return "admin/specification";
    }

    @RequestMapping("getSpList")
    @ResponseBody
    public Result getSpList(){
        try{
            List<TbSpecification> brandList = specificationService.getSpList();
            return Result.response(ResultEnum.SUCCESS).setData(brandList);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("spPage")
    @ResponseBody
    public Result brandPage(@RequestParam("specName")String specName,@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "3") Integer pageSize) {
        try {
            Page<TbSpecification> page = specificationService.spPage(specName,pageNumber, pageSize);
            return Result.response(ResultEnum.SUCCESS).setData(page);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("addModal")
    @ResponseBody
    public Result addModal(TbSpecification tbSpecification, @RequestParam("optionName") String[] optionName, @RequestParam("orders") Integer[] orders){
        try{
             Boolean b = specificationService.addModal(tbSpecification,optionName,orders);
            return Result.response(ResultEnum.SUCCESS).setData(b);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    /*批量删除*/
    @RequestMapping("deleteInfo")
    @ResponseBody
    public Result deleteInfo(@RequestParam("id[]") Long[] id){
        try {
            Boolean b = specificationService.deleteInfo(id);
            return Result.response(ResultEnum.SUCCESS).setData(b);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    /*删除*/
    @RequestMapping("deleteById")
    @ResponseBody
    public Result deleteById(@RequestParam("id") Long id){
        try {
            Boolean b = specificationService.deleteById(id);
            return Result.response(ResultEnum.SUCCESS).setData(b);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("getSpecById")
    @ResponseBody
    public Result getSpecById(Long id){
        try {
            SpecVO specVO = specificationService.getSpecById(id);
            return Result.response(ResultEnum.SUCCESS).setData(specVO);
        } catch (Exception e) {
            e.fillInStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    //修改
    @RequestMapping("updateModal")
    @ResponseBody
    public Result updateModal(TbSpecification tbSpecification, @RequestParam("optionName") String[] optionName, @RequestParam("orders") Integer[] orders){
        try{
            Boolean b = specificationService.updateModal(tbSpecification,optionName,orders);
            return Result.response(ResultEnum.SUCCESS).setData(b);
        }catch (Exception e){
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }


}
